package com.kingyun.gpsinspection.purificationservice.services.jsm.outerservice.impl;

import com.kingyun.gpsinspection.purificationservice.common.enums.TreeIconSkinEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.JsmOrganizationMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.OutEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.employee.JsmEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.organization.JsmOrganizationService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmAccountRoleService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmRolesService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.post.JsmPostService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zmling on 2016/12/14.
 */

/**
 * 人员信息
 */
public class OutEmployeeServiceImpl implements OutEmployeeService {
    @Autowired
    private JsmEmployeeService jsmEmployeeService;

    @Autowired
    private JsmPostService postService;
    @Autowired
    private JsmOrganizationService organizationService;
    @Autowired
    private JsmRolesService roleService;
    @Autowired
    private JsmAccountRoleService accountRoleService;
    @Autowired
    private JsmOrganizationService jsmOrganizationService;
    @Autowired
    private JsmOrganizationMapper jsmOrganizationMapper;

    /**
     * 查询所有人员信息
     *
     * @param employee
     * @return
     * @throws Exception
     */
    @Override
    public List<EmployeeVo> findEmployeeList(Employee employee) throws Exception {
        return jsmEmployeeService.findEmployees(employee);
    }

    public List<EmployeeVo> findEmployeeByLoginId(Employee employee) throws Exception {
        List<EmployeeVo> list = new ArrayList<>();
        List<EmployeeVo> voList = jsmEmployeeService.queryList(employee);
        if (voList != null && voList.size() > 0)
            for (EmployeeVo employeeVo : voList) {
                // 判断当前类型
                if (employeeVo.getPostId() == null) {
                    employeeVo.setPostName("--");
                } else {
                    postVo postVo = postService.toPostView(employeeVo.getPostId());
                    // 操作岗位
                    if (postVo != null && postVo.getPostName() != null) {
                        employeeVo.setPostName(postVo.getPostTypeName());
                    }

                }
                // 根据组织机构获取作业区
                if (employeeVo.getOrgId() != null) {
                    Organization organization = organizationService.selectOrgByOrgId(employeeVo.getOrgId());
                    /**
                     * 1,2,3
                     */
                    if (Integer.parseInt(organization.getOrgLevelId()) <= 3) {
                        employeeVo.setShowOrgName(organization.getOrgName());
                    } else {
                        List<Organization> organizationList = organizationService.getOrganizationList(employeeVo);
                        if (organizationList != null && organizationList.size() > 0) {
                            for (Organization org : organizationList) {
                                if (org.getOrgLevelId().equals("3")) {
                                    employeeVo.setShowOrgName(org.getOrgName());
                                }
                            }
                        }
                    }
                }
                // 查询当前系统角色
                String roleName = "";

                List<AccountRole> accountRoles = accountRoleService.getRolesByAccount(employeeVo);
                if (accountRoles != null && accountRoles.size() > 0) {
                    int splitInt = 0;
                    for (AccountRole accountRole : accountRoles) {
                        Roles roles = roleService.rolesSelect(accountRole.getRoleId());
                        //
                        if (roles != null && roles.getRoleName() != null) {
                            if (splitInt == 0) {
                                roleName = roles.getRoleName();
                            } else {
                                roleName = roleName + "," + roles.getRoleName();
                            }
                        }
                        splitInt++;
                    }
                }
                employeeVo.setRolesName(roleName);
                list.add(employeeVo);
            }
        return list;
    }

    /**
     * 加载人员混合树
     *
     * @param orgId
     * @param sync
     * @param openOrg
     * @param openDept
     * @param keyword
     * @return
     * @throws Exception
     */
    @Override
    public List<Ztree> loadEmployeeZtree(String orgId, boolean sync, boolean openOrg, boolean openDept, String keyword) throws Exception {
        return jsmEmployeeService.loadEmployeeZtree(orgId, sync, openOrg, openDept, keyword);
    }

    /**
     * @param orgId              = null 查询全部组织机构
     * @param isLoadOrg          = true  加载组织树
     * @param isLoadDept         = true 加载组织部门树
     * @param isLoadOrgEmployee  = true 加载组织机构人员
     * @param isLoadDeptEmployee = true 加载组织部门人员
     * @param keyword            搜索值
     * @param maxLevel           获取最大层级{气矿分公司，等其他问题}
     * @param lessLevel          获取最小层级
     */
    @Override
    public List<Ztree> loadEmployeeZtree(String orgId, boolean isLoadOrg, boolean isLoadDept, boolean isLoadOrgEmployee, boolean isLoadDeptEmployee, String maxLevel, String lessLevel, String keyword) throws Exception {
        return jsmEmployeeService.loadEmployeeZtree(orgId, isLoadOrg, isLoadDept, isLoadOrgEmployee, isLoadDeptEmployee, maxLevel, lessLevel, keyword);
    }


    /**
     * 根据ID查询信息
     *
     * @param employeeId
     * @return Employee
     * @Author 卢曼成
     */
    @Override
    public Employee findById(String employeeId) {
        return jsmEmployeeService.queryByKeyId(employeeId);
    }

    /**
     * 根据组织id查询出顶层组织id，如果没有则返回当前查询的id
     *
     * @param OrgId 组织id
     */
    @Override
    public String queryTopOrgByOrgId(String OrgId) {
        List<Organization> list = jsmOrganizationService.selectUpByTree(OrgId);
        for (Organization organization : list) {
            if (organization.getOrgLevelId().equals("3")) {
                OrgId = organization.getOrgId();
                break;
            }
        }
        return OrgId;
    }

    /**
     * 根据组织id查询备选人
     *
     * @param OrgId
     */
    @Override
    public List<Ztree> queryCandidateByOrgId(String OrgId) {
        List<Ztree> zTrees = new ArrayList<>();
        List<Candidate> list = jsmOrganizationMapper.queryCandidateByOrgId(OrgId);
        if (null != list && list.size() > 0) {
            Ztree zt = new Ztree();
            zt.setId(list.get(0).getOrgId());
            zt.setName(list.get(0).getOrgName());
            zt.setIconSkin(TreeIconSkinEnum.getDes("9"));
            zt.setIsParent(true);
            zt.setOpen(true);
            zTrees.add(zt);
            for (Candidate candidate : list) {
                Ztree ztree = new Ztree();
                ztree.setId(candidate.getEmployeeId());
                ztree.setName(candidate.getEmployeeName());
                ztree.setIconSkin(TreeIconSkinEnum.ICON_PEOPLE.getDesc());
                ztree.setpId(candidate.getOrgId());
                zTrees.add(ztree);
            }
        }
        return zTrees;
    }
}
